package org.redemptionhost.web.panels;

import java.io.Serializable;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.redemptionhost.GreetingQueries;
import org.redemptionhost.HostQueries;
import org.redemptionhost.model.Greeting;
import org.redemptionhost.model.Host;

import com.google.inject.Inject;

public class ListHostPanel extends Panel implements Serializable {
	private static final long serialVersionUID = 1L;
	
    @Inject
    private HostQueries hostQueries;
	
	public ListHostPanel(String id) {
		super(id);
		
        // This LoadableDetachableModel allows the following ListView<Greeting> to always load the latest persisted
        // Greeting entities on-demand, without having to store any model data in the session when this Guestbook page
        // gets serialized.
        LoadableDetachableModel<List<Host>> allHosts = new LoadableDetachableModel<List<Host>>()
        {
			private static final long serialVersionUID = 1L;

			@Override
            protected List<Host> load()
            {
                return hostQueries.all();
            }
        };

        ListView<Host> hosts= new ListView<Host>("hostList", allHosts)
        {
            @Override
            protected void populateItem(ListItem<Host> item)
            {
                Host host = item.getModel().getObject();
                item.add(new Label("name", host.getFirstName() + " " + host.getLastName()));
                item.add(new Label("address", host.getCity() + ", " + host.getState()));
                item.add(new Label("email", host.getEmail()));
                item.add(new Label("phone", host.getPhone()));
            }
        };
        add(hosts); 
	}

}
